TWB.New_Module({
	///////////////////////////
	name : "FullUpdate",	 //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////

	settings : {
		howold : {
			type : "text"
		}
	},
	Ask : function(_set){
		var ar=[[],[]];
		var result=[];
		switch(_set){
			case 0: // Current
				ar[0].push(TWB.lang("builds"));
				ar[1].push("CurrentBuilds");
				
				ar[0].push(TWB.lang("techs"));
				ar[1].push("CurrentTechs");
				
				ar[0].push(TWB.lang("units"));
				ar[1].push("CurrentUnits");
				
				//ar[0].push(TWB.lang("moves"));
				//ar[1].push("CurrentMoves");
			break;

			case 1 : // Outdate 
				ar[0].push(TWB.lang("builds"));
				ar[1].push("OutdatedBuilds");
				
				ar[0].push(TWB.lang("techs"));
				ar[1].push("OutdatedTechs");
				
				ar[0].push(TWB.lang("units"));
				ar[1].push("OutdatedUnits");
			break;
			
			case 3 : // All
				ar[0].push(TWB.lang("villagelist"));
				ar[1].push("AllVillageList");
				
				ar[0].push(TWB.lang("builds"));
				ar[1].push("AllBuilds");
				
				ar[0].push(TWB.lang("techs"));
				ar[1].push("AllTechs");
				
				ar[0].push(TWB.lang("units"));
				ar[1].push("AllUnits");

				ar[0].push(TWB.lang("groups"));
				ar[1].push("AllGroups");
				
				ar[0].push(TWB.lang("reports"));
				ar[1].push("AllReports");
				
				//ar[0].push(TWB.lang("moves"));
				//ar[1].push("AllMoves");
			break;
		}
		
		params={inn:ar[0],out:{}};
		selected=TWB.get_var("update_cache"+_set,[]);
		window.openDialog("chrome://twbooster/content/update.xul", "","modal,chrome,dialog,resizable=yes,alwaysRaised", TWB.lang("update"), params, selected);
		if(params.out){
			TWB.set_var("update_cache"+_set,params.out);
		}
		else{
			TWB.set_var("update_cache"+_set,[]);
		}
		var i;
		for(i=0; i<params.out.length; i++){
			result.push(ar[1][params.out[i]]);
		}
		return result;
	},	
	//[START]-------Current-Functions-------//
	Current : function(){
		if(TWB.logged(window._content)){
			TWB.full_update=TWB.Modules.FullUpdate.Ask(0);
			var vil_el=TWB.Xpath("//td[@id='menu_row2_village']",9);
			var vil_id=TWB.Modules.BackNext.GetVil();
			if(!TWB.gei("TWB-v"+vil_id)){
				// Add current village to village
				TWB.Modules.Villages.NewVil({
					id : "TWB-v"+vil_id,
					label : vil_el.textContent,
					tooltiptext : vil_el.nextSibling.textContent,
					value : vil_el.textContent+" "+vil_el.nextSibling.textContent
				});
			}
			// Update current village
			TWB.Modules.Villages.UpdateCurVil();
			// Start the first function
			if(typeof TWB.full_update[0]!="undefined"){
				TWB.Modules.FullUpdate[TWB.full_update[0]]();
			}
			else{
				TWB.Modules.FullUpdate.CurrentFinish();
			}
		}
		else{
			alert(TWB.lang("notlogged"));
		}
	},
	CurrentNext : function(){
		TWB.full_update.splice(0,1);
		if(typeof TWB.full_update[0]!="undefined"){
			TWB.Modules.FullUpdate[TWB.full_update[0]]();
		}
		else{
			TWB.Modules.FullUpdate.CurrentFinish();
		}
	},
	CurrentBuilds : function(){
		TWB.Modules.FullUpdate.CurrentNext();
	},
	CurrentTechs : function(){
		if(TWB.Modules.Icons.Screen()!="smith"){
			TWB.Processor.start("current_techs","Modules.FullUpdate.CurrentTechs","*",true);
			TWB.Modules.Icons.GoTo("smith");
		}
		else{
			TWB.Modules.FullUpdate.CurrentNext();
		}
	},
	CurrentUnits : function(){
		if(TWB.Modules.Icons.Screen()!="place"){
			TWB.Processor.start("current_units","Modules.FullUpdate.CurrentUnits","*",true);
			TWB.Modules.Icons.GoTo("place");
		}
		else{
			TWB.Modules.FullUpdate.CurrentNext();
		}
	},
	CurrentMoves : function(){
		if(TWB.Modules.Icons.Screen()!="place"){
			TWB.Processor.start("current_moves","Modules.FullUpdate.CurrentMoves","*",true);
			TWB.Modules.Icons.GoTo("place");
		}
		else{
			// Also move this to scraper
			var vil_id=TWB.Modules.BackNext.GetVil();
			if(typeof TWB._SESSION.MOVES=="undefined"){
				TWB._SESSION.MOVES={};
			}
			if(typeof TWB._SESSION.MOVES[vil_id]=="undefined"){
				TWB._SESSION.MOVES[vil_id]={};
			}
			
			
			//var own=TWB.Xpath("//a[contains(@href,'type=own')]",6);
			
			
			//............... GRAB ALL INCOMINGS AND OUTGOINGS ANY TYPE 
			
			
			TWB.Modules.FullUpdate.CurrentNext();
		}
	},
	CurrentFinish : function(){
		delete TWB.full_update;
		alert(TWB.lang("updatedone"));
	},
	//[END]-------Current-Functions-------//
	//[START]-------All-Functions-------//
	//Percentage using MoodleR's idea and update without keeping it hanging!
	All : function(){
		if(TWB.logged(window._content)){
			TWB.full_update=TWB.Modules.FullUpdate.Ask(3);
			// Start the first function
			if(typeof TWB.full_update[0]!="undefined"){
				TWB.Processor.start(TWB.full_update[0].toLowerCase(),"Modules.FullUpdate."+TWB.full_update[0],"*",false);
				TWB.Modules.FullUpdate[TWB.full_update[0]]();
			}
			else{
				TWB.Modules.FullUpdate.AllFinish();
			}
		}
		else{
			alert(TWB.lang("notlogged"));
		}
	},
	AllNext : function(){
		TWB.full_update.splice(0,1);
		if(typeof TWB.full_update[0]!="undefined"){
			// Add it as a process and launch
			TWB.Processor.start(TWB.full_update[0].toLowerCase(),"Modules.FullUpdate."+TWB.full_update[0],"*",false);
			TWB.Modules.FullUpdate[TWB.full_update[0]]();
		}
		else{
			TWB.Modules.FullUpdate.AllFinish();
		}
	},
	AllVillageList : function(){
		if(TWB.Modules.Icons.Screen()!="overview_villages" || TWB.Modules.Icons.Mode()!="prod"){
			TWB.Processor.start("all_villagelist","Modules.FullUpdate.AllVillageList","*",true);
			TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","prod",true)+"&group=0");
		}
		else{
			TWB.Modules.Villages.Update();
			TWB.Modules.FullUpdate.AllNext();
		}
	},
	AllBuilds : function(){
		if(TWB.Modules.Icons.Screen()!="overview"){
			TWB.all_builds=0;
			TWB.Modules.Icons.GoTo("overview");
		}
		else{
			var V=TWB.gei("TWB-VillageList");
			// If we are not in the right village
			if(V.childNodes[TWB.all_builds].id.split("TWB-v")[1]!=TWB.Modules.BackNext.GetVil()){
				TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_builds].id.split("TWB-v")[1]);
			}
			else{
				TWB.all_builds++;
				var V=TWB.gei("TWB-VillageList");
				if(typeof V.childNodes[TWB.all_builds]!="undefined"){
					TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_builds].id.split("TWB-v")[1]);
				}
				else{
					// Finished this
					delete TWB.all_builds;
					TWB.Processor.stop("allbuilds");
					TWB.Modules.FullUpdate.AllNext();
				}
			}
		}
	},
	AllTechs : function(){
		if(TWB.Modules.Icons.Screen()!="smith"){
			TWB.all_techs=0;
			TWB.Modules.Icons.GoTo("smith");
		}
		else{
			// If we are not in the right village
			var V=TWB.gei("TWB-VillageList");
			if(V.childNodes[TWB.all_techs].id.split("TWB-v")[1]!=TWB.Modules.BackNext.GetVil()){
				TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_techs].id.split("TWB-v")[1]);
			}
			else{
				TWB.all_techs++;
				var V=TWB.gei("TWB-VillageList");
				if(typeof V.childNodes[TWB.all_techs]!="undefined"){
					TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_techs].id.split("TWB-v")[1]);
				}
				else{
					// Finished this
					delete TWB.all_techs;
					TWB.Processor.stop("alltechs");
					TWB.Modules.FullUpdate.AllNext();
				}
			}
		}
	},
	AllUnits : function(){
		if(TWB.Modules.Icons.Screen()!="place"){
			TWB.all_units=0;
			TWB.Modules.Icons.GoTo("place");
		}
		else{
			// If we are not in the right village
			var V=TWB.gei("TWB-VillageList");
			if(V.childNodes[TWB.all_units].id.split("TWB-v")[1]!=TWB.Modules.BackNext.GetVil()){
				TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_units].id.split("TWB-v")[1]);
			}
			else{			
				TWB.all_units++;
				var V=TWB.gei("TWB-VillageList");
				if(typeof V.childNodes[TWB.all_units]!="undefined"){
					TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_units].id.split("TWB-v")[1]);
				}
				else{
					// Finished this
					delete TWB.all_units;
					TWB.Processor.stop("allunits");
					TWB.Modules.FullUpdate.AllNext();
				}
			}
		}
	},
	AllMoves : function(){
		if(TWB.Modules.Icons.Screen()!="place"){
			TWB.all_moves=0;
			TWB.Modules.Icons.GoTo("place");
		}
		else{
			// If we are not in the right village
			var V=TWB.gei("TWB-VillageList");
			if(V.childNodes[TWB.all_moves].id.split("TWB-v")[1]!=TWB.Modules.BackNext.GetVil()){
				TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_moves].id.split("TWB-v")[1]);
			}
			else{
				// Move this to scraper
				var vil_id=TWB.Modules.BackNext.GetVil();
				var WN=TWB._window();
				if(typeof WN.wrappedJSObject!="undefined"){
					WN=WN.wrappedJSObject;
				}
				if(typeof TWB._SESSION.MOVES=="undefined"){
					TWB._SESSION.MOVES={};
				}
				if(typeof TWB._SESSION.MOVES[vil_id]=="undefined"){
					TWB._SESSION.MOVES[vil_id]={};
				}
				
				
				
				
				
				TWB.all_moves++;
				var V=TWB.gei("TWB-VillageList");
				if(typeof V.childNodes[TWB.all_moves]!="undefined"){
					TWB.Modules.BackNext.LoadVil(V.childNodes[TWB.all_moves].id.split("TWB-v")[1]);
				}
				else{
					// Finished this
					delete TWB.all_moves;
					TWB.Processor.stop("allmoves");
					TWB.Modules.FullUpdate.AllNext();
				}
			}
		}
	},
	AllGroups : function(){
		if(TWB.Modules.Icons.Screen()!="overview_villages" || TWB.Modules.Icons.Mode()!="groups"){
			TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","groups",true)+"&group=0");
		}
		else{
			var i;
			var groups=TWB.Xpath("//*[@id='paged_view_content']/table/tbody/tr/td/a[not(contains(@href,'page'))]",6);
			// Start or die
			if(groups.length>0){
				TWB.Modules.FullUpdate.all_groups=[];
				for(i=0; i<groups.length; i++){
					TWB.Modules.FullUpdate.all_groups.push(groups[i].href.match(/group=(\d+)/)[1]);
				}
				
				TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","groups",true)+"&group="+TWB.Modules.FullUpdate.all_groups[0]);
				TWB.Processor.start("allgroups_","Modules.FullUpdate.AllGroups_","*",false);
			}
			else{
				// Finished this
				TWB.Modules.FullUpdate.AllNext();				
			}
			TWB.Processor.stop("allgroups");
		}
	},
	AllGroups_ : function(){
		TWB.Modules.FullUpdate.all_groups.splice(0,1);
		if(TWB.Modules.FullUpdate.all_groups.length>0){
			TWB.Load_Url(TWB.Modules.Icons.GoTo("overview_villages","groups",true)+"&group="+TWB.Modules.FullUpdate.all_groups[0]);
		}
		else{
			// Finished this
			delete TWB.Modules.FullUpdate.all_groups;
			TWB.Processor.stop("allgroups_");
			TWB.Modules.FullUpdate.AllNext();
		}
	},
	AllReports : function(){
		if(TWB.Modules.Icons.Screen()!="report" || TWB.Modules.Icons.Mode()!="all"){
			TWB.Load_Url(TWB.Modules.Icons.GoTo("report","all",true)+"&from=0");
		}
		else{
			if(typeof TWB.Modules.FullUpdate.all_reports=="undefined"){
				TWB.Modules.FullUpdate.all_reports=[];
			}

			var i;
			var links=TWB.Xpath("//a[contains(@href,'&view=')]",6);
			for(i=0; i<links.length; i++){
				TWB.Modules.FullUpdate.all_reports.push(links[i].href.match(/view=(\d+)/)[1]);
			}
			
			var cur_page=(TWB.URL().match(/from=(\d+)/) || [0,0])[1]/12;
			var pages=TWB.Xpath("//a[contains(@href,'from')]",6);
			if(cur_page==pages.length){
				// Start or die
				if(TWB.Modules.FullUpdate.all_reports.length>0){
					TWB.Load_Url(TWB.Modules.Icons.GoTo("report","all",true)+"&view="+TWB.Modules.FullUpdate.all_reports[0]);
					TWB.Processor.start("allreports_","Modules.FullUpdate.AllReports_","*",false);
				}
				else{
					// Finished this
					delete TWB.all_reports;
					TWB.Modules.FullUpdate.AllNext();				
				}
				TWB.Processor.stop("allreports");
			}
			else{
				cur_page++;
				cur_page*=12;
				TWB.Load_Url(TWB.Modules.Icons.GoTo("report","all",true)+"&from="+cur_page);
			}
		}
	},
	AllReports_ : function(){
		TWB.Modules.FullUpdate.all_reports.splice(0,1);
		if(TWB.Modules.FullUpdate.all_reports.length>0){
			TWB.Load_Url(TWB.Modules.Icons.GoTo("report","all",true)+"&view="+TWB.Modules.FullUpdate.all_reports[0]);
		}
		else{	
			// Finished this
			delete TWB.Modules.FullUpdate.all_reports;
			TWB.Processor.stop("allreports_");
			TWB.Modules.FullUpdate.AllNext();
		}
	},
	AllFinish : function(){
		delete TWB.full_update;
		alert(TWB.lang("updatedone"));
	},
	//[END]-------All-Functions-------//
	//[START]-------Out-Functions-------//
	Outdated : function(){
		var i;
		var vil;
		TWB.Modules.FullUpdate.outdated_links=[];
		var tasks=TWB.Modules.FullUpdate.Ask(1);
		for(i=0; i<tasks.length; i++){
			switch(tasks[i]){
				case "OutdatedBuilds":
					if(typeof TWB._SESSION.BUILDS!="undefined"){
						for(vil in TWB._SESSION.BUILDS){
							if(TWB.CheckTime(TWB._SESSION.BUILDS[vil].time,TWB.get_var("howold")*24*3600)){
								TWB.Modules.FullUpdate.outdated_links.push(TWB.Modules.Icons.GoTo("overview",undefined,true).replace(/village=(\d+)/,"village="+vil));
							}
						}
					}
				break;
				
				case "OutdatedTechs":
					if(typeof TWB._SESSION.TECHS!="undefined"){
						if(typeof TWB._SESSION.TECHS!="undefined"){
							for(vil in TWB._SESSION.TECHS){
								if(TWB.CheckTime(TWB._SESSION.TECHS[vil].time,TWB.get_var("howold")*24*3600)){
									TWB.Modules.FullUpdate.outdated_links.push(TWB.Modules.Icons.GoTo("smith",undefined,true).replace(/village=(\d+)/,"village="+vil));
								}
							}
						}
					}
				break;
				
				case "OutdatedUnits":
					if(typeof TWB._SESSION.UNITS!="undefined"){
						if(typeof TWB._SESSION.UNITS!="undefined"){
							for(vil in TWB._SESSION.UNITS){
								if(TWB.CheckTime(TWB._SESSION.UNITS[vil].time,TWB.get_var("howold")*24*3600)){
									TWB.Modules.FullUpdate.outdated_links.push(TWB.Modules.Icons.GoTo("place",undefined,true).replace(/village=(\d+)/,"village="+vil));
								}
							}
						}
					}
				break;
			}
		}
		TWB.Processor.start("outdated_loop","Modules.FullUpdate.OutdatedNext","*",false);
		TWB.Modules.FullUpdate.OutdatedNext();
	},
	OutdatedNext : function(){
		if(TWB.Modules.FullUpdate.outdated_links.length>0){
			TWB.Load_Url(TWB.Modules.FullUpdate.outdated_links[0]);
			TWB.Modules.FullUpdate.outdated_links.splice(0,1);
		}
		else{
			TWB.Modules.FullUpdate.OutdatedFinish();
		}
	},
	OutdatedFinish : function(){
		TWB.Processor.stop("outdated_loop");
		delete TWB.Modules.FullUpdate.outdated_links;
		alert(TWB.lang("updatedone"));
	}
	//[END]--------Out-Functions--------//
});